<?php

class MprankApi extends ApiBase
{
	private $mysql = null;

	public function __construct()
	{
		parent::__construct();
		$this->mysql = n_api('mysql')->cname('weixin');
		$this->redis = n_api('redis')->scrapy;
	}

	public function getMprankByCategoryId($cid, $date, $type, $start, $perpage)
	{
		// $key = "expire:mprank:getMprankByCategoryId:$cid:$date:$type:$start:$perpage";
		// $categoryMprank = $this->redis->get($key);

		// if (!$categoryMprank) {
			$rs = n_api('weixin')->getWxidsByCategoryIdOnlyId($cid);
			$wxids = [];
			foreach ($rs as $value) {
				$wxids[] = "'{$value['wxid']}'";
				$wxInfo[$value['wxid']] = $value['wxname'];
			}


			if (count($wxids) == 0)
				return [];

			if ($type == 'day'){
				$query = "select * from mprank_day where daytime = $date and wxid in (" . implode(',', $wxids) . ") order by weixin_index desc limit $start, $perpage";
			}
			else if ($type == 'week') {
				$stime = $date;
				$etime = $date + 7 * 24 * 3600;
				$query = "select * from mprank_week where weektime >=$stime and weektime < $etime and wxid in (" . implode(',', $wxids) . ") order by weixin_index desc limit $start, $perpage";
			} else if ($type == 'maxday') {
				$query = "select * from mprank_day where daytime = (select max(daytime) from mprank_day) and wxid in (" . implode(',', $wxids) . ") order by weixin_index desc limit $start, $perpage";
			}

			// if ($type == 'day') {
			// 	$query = "select * from mprank_day where daytime = $date and wxid in (" . implode(',', $wxids) . ") order by weixin_index desc limit $start, $perpage";
			// } else if ($type == 'week') {
			// 	$query = "select * from mprank_week where weektime = $date and wxid in (" . implode(',', $wxids) . ") order by weixin_index desc limit $start, $perpage";
			// } else if ($type == 'maxday') {
			// 	$query = "select * from mprank_day where daytime = (select max(daytime) from mprank_day) and wxid in (" . implode(',', $wxids) . ") order by weixin_index desc limit $start, $perpage";
			// }

			$rs = $this->mysql->select($query);
			foreach ($rs as $value) {
				$value['wxname'] = $wxInfo[$value['wxid']];
				$data[] = $value;
			}
			
			return $data;
		// 	$categoryMprank = json_encode($data);

		// 	$this->redis->set($key, $categoryMprank);
		// }

		// return json_decode($categoryMprank, true);
	}
	public function getMprankByCategoryIdBetween($cid, $stime, $etime, $start, $perpage)
	{
		// $key = "expire:mprank:getMprankByCategoryId:$cid:$date:$type:$start:$perpage";
		// $categoryMprank = $this->redis->get($key);
		// if (!$categoryMprank) {
			$rs = n_api('weixin')->getWxidsByCategoryIdOnlyId($cid);
			$wxids = [];
			foreach ($rs as $value) {
				$wxids[] = "'{$value['wxid']}'";
				$wxInfo[$value['wxid']] = $value['wxname'];
			}


			if (count($wxids) == 0)
				return [];

			
			//echo $query = "select * from mprank_day where daytime >=$stime and daytime < $etime and wxid in (" . implode(',', $wxids) . ") limit $start, $perpage";

			$query = "select wxid,sum(all_read_num) as all_read_num_sum,
									round(avg(all_read_num)) as all_read_num,
									round(avg(all_like_num)) as all_like_num,
									round(avg(head_read_num)) as head_read_num,
									round(avg(average_read_num)) as average_read_num,
									max(max_read_num) as max_read_num,
									round(avg(weixin_index)) as weixin_index,
									sum(weixin_index) as weixin_index_sum
									from mprank_day 
									where daytime >=$stime and daytime < $etime and wxid in (" . implode(',', $wxids) . ") 
									group by wxid 
									order by weixin_index desc 
									limit $start, $perpage";

			// if ($type == 'day') {
			// 	$query = "select * from mprank_day where daytime = $date and wxid in (" . implode(',', $wxids) . ") order by weixin_index desc limit $start, $perpage";
			// } else if ($type == 'week') {
			// 	$query = "select * from mprank_week where weektime = $date and wxid in (" . implode(',', $wxids) . ") order by weixin_index desc limit $start, $perpage";
			// } else if ($type == 'maxday') {
			// 	$query = "select * from mprank_day where daytime = (select max(daytime) from mprank_day) and wxid in (" . implode(',', $wxids) . ") order by weixin_index desc limit $start, $perpage";
			// }

			$rs = $this->mysql->select($query);
			foreach ($rs as $value) {
				$value['wxname'] = $wxInfo[$value['wxid']];
				$data[] = $value;
			}
			
			return $data;
		// 	$categoryMprank = json_encode($data);

		// 	$this->redis->set($key, $categoryMprank);
		// }

		// return json_decode($categoryMprank, true);
	}
	public function getMprankByWxids(array $wxids, $date, $start=0, $limit=50)
	{
		foreach ($wxids as $wxid) {
			$wxidStr[] = "'$wxid'";
		}
		$query = "select * from mprank_day where daytime = $date and wxid in (" . implode(',', $wxidStr) . ") order by weixin_index desc limit $start, $limit";
		$data = $this->mysql->select($query);
		$wxInfo = n_api('weixin')->getwxInfo($wxids);
		foreach($data as $key=>$val){
			$item = $val;
			$item['wxname'] = $wxInfo[$val['wxid']]['wxname'];
			$data[$key] = $item;
		}
		return $data;
	}
	public function getMprankByWxidsCount(array $wxids, $date, $start=0, $limit=50)
	{
		foreach ($wxids as $wxid) {
			$wxidStr[] = "'$wxid'";
		}
		$query = "select count(*) from mprank_day where daytime = $date and wxid in (" . implode(',', $wxidStr) . ")";
		return $this->mysql->count($query);
		
	}
	public function getMprankWeeklyByWxids($wxids, $date, $start=0, $limit=50)
	{
		$etime = $date;
		$stime = $etime - 7*24*60*60;
		foreach ($wxids as $wxid) {
			$wxidStr[] = "'$wxid'";
		}
		$wxInfo = n_api('weixin')->getwxInfo($wxids);

		$query = "select * from mprank_day where daytime > $stime and daytime <= $etime and wxid in (" . implode(',', $wxidStr) . ") order by weixin_index desc limit $start, $limit";
		$data = $this->mysql->select($query);
		foreach ($data as $value) {
			$tmp[$value['wxid']]['wxid'] = $value['wxid'];
			$tmp[$value['wxid']]['wxname'] = $wxInfo[$value['wxid']]['wxname'];
			$tmp[$value['wxid']]['all_read_num'] += $value['all_read_num'];
			$tmp[$value['wxid']]['all_like_num'] += $value['all_like_num'];
			$tmp[$value['wxid']]['head_read_num'] += $value['head_read_num'];
			$tmp[$value['wxid']]['average_read_num'] += $value['average_read_num'];
			$tmp[$value['wxid']]['max_read_num'] += $value['max_read_num'];
			$tmp[$value['wxid']]['weixin_index'] += $value['weixin_index'];
			$tmp[$value['wxid']]['pub_num'] += $value['pub_num'];
		}
		foreach($tmp as $key=>$val){
			$index[$key] = $val['weixin_index'];
		}
		arsort($index);
		foreach($index as $key=>$val){
			$tmp[$key]['weixin_index'] = round($tmp[$key]['weixin_index']/7,2);
			$rs[$key] = $tmp[$key];
		}
		//print_r($rs);
		return $rs;
	}
	public function getMprankWeeklyByWxidsBetween($wxids, $stime,$etime)
	{

		foreach ($wxids as $wxid) {
			$wxidStr[] = "'$wxid'";
		}
		$wxInfo = n_api('weixin')->getwxInfo($wxids);

		$query = "select * from mprank_day where daytime > $stime and daytime <= $etime and wxid in (" . implode(',', $wxidStr) . ")";
		$data = $this->mysql->select($query);
		foreach ($data as $value) {
			$tmp[$value['wxid']]['wxid'] = $value['wxid'];
			$tmp[$value['wxid']]['wxname'] = $wxInfo[$value['wxid']]['wxname'];
			$tmp[$value['wxid']]['all_read_num'] += $value['all_read_num'];
			$tmp[$value['wxid']]['all_like_num'] += $value['all_like_num'];
			$tmp[$value['wxid']]['head_read_num'] += $value['head_read_num'];
			$tmp[$value['wxid']]['average_read_num'] += $value['average_read_num'];
			$tmp[$value['wxid']]['max_read_num'] += $value['max_read_num'];
			$tmp[$value['wxid']]['weixin_index'] += $value['weixin_index'];
			$tmp[$value['wxid']]['pub_num'] += $value['pub_num'];
		}
		foreach($tmp as $key=>$val){
			$index[$key] = $val['weixin_index'];
		}
		arsort($index);
		foreach($index as $key=>$val){
			$tmp[$key]['weixin_index'] = round($tmp[$key]['weixin_index']/7,2);
			$rs[$key] = $tmp[$key];
		}
		//print_r($rs);
		return $rs;
	}
	
	
	public function getMaxRankDay()
	{
		// $key = 'expire:mprank:getMaxRankDay';
		// $maxday = $this->redis->get($key);
		// if (!$maxday) {
			$query = 'select max(daytime) as day from mprank_day';
			$rs = $this->mysql->fetchFirst($query);
			$maxday = $rs['day'];

		// 	$this->redis->set($key, $maxday);
		// }

		return $maxday;
	}

	public function getMaxRankWeek()
	{
		$query = "select max(weektime) as weektime from mprank_week";
		$rs = $this->mysql->fetchFirst($query);
		return $rs['weektime'];
	}

	public function getRankByDay($wxid, $time=0)
	{
		$query = "select all_read_num, all_like_num, average_read_num, max_read_num, weixin_index from mprank_day where wxid='$wxid' order by daytime desc";
		$rs = $this->mysql->fetchFirst($query);
		return $rs;
	}

	public function getRankByWeek($wxid, $time=0)
	{
		$query = "select sum(all_read_num) as read_sum, sum(all_like_num) as like_sum from mprank_day where wxid='$wxid'";
		$rs = $this->mysql->fetchFirst($query);
		return $rs;
	}

	public function getWxIndexByWeek($wxid)
	{
		//$stime = strtotime('-2 sunday');
		//$etime = strtotime('-1 sunday');
		$etime = $this->getMaxRankDay();
		$stime = $etime - 7 * 24 * 3600;

		$query = "select * from mprank_day where daytime > $stime and daytime <= $etime and wxid = '$wxid'";
		$rs = $this->mysql->select($query);
		foreach ($rs as $value) {
			$index[date("Y-m-d",$value['daytime'])] = $value;
		}
		return $index;
	}

	public function getDiyByPpid($ppid)
	{
		$query = "select * from mprank_diy where ppid = $ppid";
		$rs = $this->mysql->select($query);
		return $rs;
	}
}